/**
 * Created by cc
 * Description:
 * User: CZH
 * Date: 2022-09-29
 * Time: 18:55
 */

class Node{
    public int data;
    public Node next;
    public Node(int data) {
        this.data = data;
    }
}

public class MyQueueLinked {

    private Node front;
    private Node rear;
    private int usedSize;

    //入队列
    public void offer(int val) {
        Node node = new Node(val);
        if ((this.front == null) && (this.rear == null)) {
            this.front = node;
            this.rear = node;
        }else {
            this.rear.next = node;
            this.rear = node;
        }
        this.usedSize++;
    }

    //出队头元素
    public int poll() throws RuntimeException {
        if (this.usedSize == 0) {
            throw new RuntimeException("队列为空！");
        }
        int val = this.front.data;
        if (this.front.next == null) {
            this.front = null;
            this.rear = null;
        }else {
            this.front = this.front.next;
        }
        this.usedSize--;
        return val;
    }

    //查看队头元素
    public int peek() throws RuntimeException {
        if (this.usedSize == 0) {
            throw new RuntimeException("队列为空！");
        }
        int val = this.front.data;
        return val;
    }

    //判断队列是否空
    public boolean isEmpty() {
        return this.usedSize == 0;
    }

    //得到队列的元素个数
    public int size() {
        return this.usedSize;
    }
}
